//
//  FingerPrintHelper.swift
//  Monosaccharides
//
//  Created by iSolar on 2017/11/15.
//  Copyright © 2017年 nothing. All rights reserved.
//

import Foundation
import LocalAuthentication

class FingerPrintHelper: NSObject {

    @objc
    enum FPCheck: NSInteger {
        case success           //成功
        case failed            //失败
        case pwdNotSet         //没有设置密码
        case touchIDNotSet     //没有设置指纹
        case notAvailable      //不支持指纹
    }
    
    //MARK: -- 校验是否支持指纹，但是不涉及验证
    static func checkFingerPrintPWDAvailable(block : @escaping (_ result: FPCheck) -> Void) {
        
        if #available(iOS 8.0, OSX 10.12,*) {
            
            let context = LAContext()
            context.localizedFallbackTitle = ""
            var error: NSError? = nil
            
            // 判断设备是否支持指纹解锁
            if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
                
                block(FPCheck.success)
                
            }
            
            guard error != nil else {
                block(FPCheck.success)
                return
            }
            
            switch LAError(_nsError: error!).code {
            case LAError.passcodeNotSet:
                block(FPCheck.pwdNotSet)
                break
            case LAError.touchIDNotEnrolled:
                block(FPCheck.touchIDNotSet)
                break
            case LAError.touchIDNotAvailable:
                block(FPCheck.notAvailable)
                break
            default:
                
                // 所有的其他情况如果有需求，可在后边添加，一般情况下，不需要处理，返回失败
                block(FPCheck.failed)
                
                break
            }
  
        }
        
    }
    
    //MARK: -- 校验指纹是否校验成功，否则返回错误信息
    static func fingerPrintPWDResult(withMsg msg: String, block: @escaping(_ result: FPCheck) -> Void) {
        
        if #available(iOS 8.0, OSX 10.12, *) {
            
            let context = LAContext()
            context.localizedFallbackTitle = ""
            var error: NSError? = nil
            let msg: String = msg
            
            // 判断设备是否支持
            if context.canEvaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, error: &error) {
                
                //指纹验证
                context.evaluatePolicy(LAPolicy.deviceOwnerAuthenticationWithBiometrics, localizedReason: msg, reply: { (success, _) in
                    
                    if success {
                        // 验证成功，切回主线程刷新UI
                        DispatchQueue.main.async {
                            block(FPCheck.success)
                        }
                        
                    } else {
                        //根据错误code，区分失败原因
                        DispatchQueue.main.async {
                            block(FPCheck.failed)
                        }
                    }
                })
                
            } else {
                
                guard error != nil else {
                    return
                }
                switch LAError(_nsError: error!).code {
                case LAError.passcodeNotSet:
                    DispatchQueue.main.async {
                        block(FPCheck.pwdNotSet)
                    }
                    break
                    
                case LAError.touchIDNotEnrolled:
                    DispatchQueue.main.async {
                        block(FPCheck.touchIDNotSet)
                    }
                    break
                    
                case LAError.touchIDNotAvailable:
                    DispatchQueue.main.async {
                        block(FPCheck.notAvailable)
                    }
                    break
                    
                default:
                    DispatchQueue.main.async {
                        block(FPCheck.failed)
                    }
                    
                    break
                    
                }
           
            }
 
        }
        
    }
    
}
